import wordprocess
import MySQLdb
import MySQLdb.cursors
import sys
import threading
import time
#config:
appdir="D:/localhost/htdocs/WebApps"
db={'host':'localhost','user':'root','passwd':'root','db':'database'}
  


def compare(doc1,doc2):
    for i in doc1[1:]:
        for j in doc2[1:]:
            if i["simhash"].hamming_distance(j["simhash"])<=3:
                i["copylist"].append(doc2[0])
                j["copylist"].append(doc1[0])

def compareall(list):
    doclist=[]
    for filename in list:
        doclist.append(wordprocess.wordprocess(filename).shashlist)
    for i in range(len(doclist)):
        for j in range(i+1,len(doclist)):
            compare(doclist[i],doclist[j])
    return doclist

def check(id):
    wordlist=[]
    conn = MySQLdb.connect(host=db["host"], user=db["user"],\
                       passwd=db["passwd"],db=db["db"])
    cursor = conn.cursor()
    count = cursor.execute('select * from checkrelate where checkid ='+id)
    dbresult = cursor.fetchall()
    cursor.close()
    conn.close()
    for r in dbresult:  
        wordlist.append(appdir+r[2])  
    result=compareall(wordlist)
    fileid = 0
    for file in result:
        res=""
        for wordp in file[1:]:
            if len(wordp["copylist"])>0:
                copyname=''
                for i in set(wordp["copylist"]):
                    copyname += i+','
                res +='\\"'+wordp["text"].lstrip()[0:10]+"..."+\
                wordp["text"].rstrip()[-10:-1]+'\\"可能抄袭了'+\
                copyname[0:-1]+"。<br />"
        if res=="":
            res='未发现抄袭现象。'
        upconn = MySQLdb.connect(host=db["host"], user=db["user"],\
                passwd=db["passwd"],db=db["db"])
        upconn.autocommit(True)
        upcursor = upconn.cursor(cursorclass=MySQLdb.cursors.DictCursor)
        upcursor.execute("UPDATE checkrelate SET result=\""+\
                               res+"\" WHERE id="+ str(dbresult[fileid][0]))
        upcursor.close()
        upconn.close()
        fileid += 1
def dbcheck():
    conn = MySQLdb.connect(host=db["host"], user=db["user"],\
                       passwd=db["passwd"],db=db["db"])
    cursor = conn.cursor()
    count = cursor.execute('select * from `check` where result = 0')
    dbresult = cursor.fetchall()
    cursor.close()
    conn.close()
    checklist=[]
    for r in dbresult:
        checklist.append(r[0])
    for cid in checklist:
        check(str(cid))
        upconn = MySQLdb.connect(host=db["host"], user=db["user"],\
                passwd=db["passwd"],db=db["db"])
        upconn.autocommit(True)
        upcursor = upconn.cursor(cursorclass=MySQLdb.cursors.DictCursor)
        upcursor.execute("UPDATE `check` SET result=1 WHERE id="+ str(cid))
        upcursor.close()
        upconn.close()
    pass
if __name__=='__main__':
    while True:
        dbcheck()
        time.sleep(5)
